package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfomapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;


    @Override
    public void customsaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        boolean ir=roomSubmitVo.getId()!=null;
        Long roomId=roomSubmitVo.getId();
        saveOrUpdate(roomSubmitVo);
        if(ir){
            //图片删除
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomId);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //配套删除
            LambdaQueryWrapper<RoomFacility> roomfacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomfacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomId);
            roomFacilityService.remove(roomfacilityLambdaQueryWrapper);
            //属性删除
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //标签删除
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //支付方式删除
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
            //租期删除
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper =  new LambdaQueryWrapper<>();
            roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

        }
        //图片保存
        List<GraphVo> graphVoList=roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfolist = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfolist.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfolist);
        }
        //配套保存
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomfacilityInfoList = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds){
                RoomFacility roomFacility = RoomFacility.builder()
                        .facilityId(facilityInfoId).roomId(roomId).build();
                roomfacilityInfoList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomfacilityInfoList);

        }
        //属性保存
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> roomAttrValueList =new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //标签保存
        List<Long> lableIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(lableIds)){
            List<RoomLabel> roomLabelList = new ArrayList<>(lableIds.size());
            for (Long lableId : lableIds) {
                RoomLabel roomLabel = RoomLabel.builder().labelId(lableId).roomId(roomId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //支付方式保存
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId).roomId(roomId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //租期保存
        List<Long> leaseTermIds =roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds){
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .leaseTermId(leaseTermId).roomId(roomId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
    }

    @Override
    public void roomPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.roomPage(page,queryVo);
    }

    @Override
    public void removeRoomById(Long id) {

        removeById(id);
        //删除graphinfolist
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper=new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphQueryWrapper);

        //删除roomfacilityinfolist
        LambdaQueryWrapper<RoomFacility> roomFacilityInfoWrapper = new LambdaQueryWrapper<>();
        roomFacilityInfoWrapper.eq(RoomFacility::getFacilityId,id);
        roomFacilityService.remove(roomFacilityInfoWrapper);
        //删除roomattrvaluelist
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper=new LambdaQueryWrapper<>();
        roomAttrValueQueryWrapper.eq(RoomAttrValue::getAttrValueId,id);
        roomAttrValueService.remove(roomAttrValueQueryWrapper);
        // 删除roomlabelinfolist
        LambdaQueryWrapper<RoomLabel> roomLabelQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelQueryWrapper.eq(RoomLabel::getLabelId,id);
        roomLabelService.remove(roomLabelQueryWrapper);
        // 删除roompaymenttypelist
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getPaymentTypeId,id);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
        // 删除roomleasetermlist
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getLeaseTermId,id);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //查询room信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        // 查询公寓信息
        ApartmentInfo apartmentInfo= apartmentInfoMapper.selectById(roomInfo.getId());

        // 查询图片信息
        List<GraphVo> graphVoList =graphInfoMapper.queryByItemId(id,ItemType.ROOM);
        // 查询属性信息
        List<AttrValueVo> attrvalueVoList =  attrValueMapper.selectListByRoomId(id);
        // 查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        // 查询标签信息
        List<LabelInfo> labelInfoList =labelInfomapper.selectListByRoomId(id);
        // 查询支付信息
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectListByRoomId(id);
        // 查询租期信息
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);

        RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setGraphVoList(graphVoList);
        adminRoomDetailVo.setAttrValueVoList(attrvalueVoList);
        adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
        adminRoomDetailVo.setLabelInfoList(labelInfoList);
        adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
        adminRoomDetailVo.setLeaseTermList(leaseTermList);

        return adminRoomDetailVo;
    }
}




